<?php
namespace App\Models\Scopes;

use App\Models\SysDept;
use App\Models\SysUser;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Support\Facades\DB;

class DataFilterScope implements Scope {
    /**
     * Apply the scope to a given Eloquent query builder.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
     * @param  \Illuminate\Database\Eloquent\Model  $model
     * @return void
     */
    public function apply(Builder $builder, Model $model)
    {
        if($model->isFillable('create_by') == true){
            $user = SysUser::with('roles')->where('id',auth()->id())->first();
            //获取用户角色
            $user_roles = $user->roles;

            $has_all_data_scope = false;
            $dept_ids = [];
            $user_ids = [];
            $user_id = $user->id;
            if($user_id === 1){
                $has_all_data_scope = true;
                return;
            }
            //过滤权限
            foreach ($user_roles as $role){
                // 全部数据权限
                if($role->data_scope == 1){
                    $has_all_data_scope = true;
                    return;
                }
                //自定数据权限
                if($role->data_scope == 2){
                    //获取角色部门关联数据
                    $role_dept_ids = DB::table('sys_role_dept')->where('role_id', $role->id)->pluck('dept_id');
                    if($role_dept_ids){
                        $dept_ids = array_merge($dept_ids, $role_dept_ids->toArray());
                    }
                }
                //本部门数据权限
                if($role->data_scope == 3){
                    //获取用户所属部门
                    $dept_ids = array_merge($dept_ids, [ $user->dept_id ]);
                }
                //本部门及以下数据权限
                if($role->data_scope == 4){
                    //获取用户部门以及相关下属部门
                    $user_dept_and_child_dept_ids = SysDept::descendantsAndSelf($user->dept_id)->pluck('id');
                    if($user_dept_and_child_dept_ids){
                        $dept_ids = array_merge($dept_ids, $user_dept_and_child_dept_ids->toArray());
                    }
                }

                //本人数据
                if($role->data_scope == 5){
                    $user_ids = array_merge($user_ids, [ $user_id ]);
                }

                //本人数据以及我创建的数据
                if($role->data_scope == 6){
                    $tmp_user_ids = SysUser::where('create_by', $user_id)->pluck('id');
                    if($tmp_user_ids){
                        $user_ids = array_merge($user_ids, $tmp_user_ids->toArray());
                    }
                }

            }
            //部门相关关联数据
            if(count($dept_ids) > 0){
                $user_in_depts_ids = SysUser::whereIn('dept_id', $dept_ids)->pluck('id');
                if($user_in_depts_ids){
                    $user_ids = array_merge($user_ids, $user_in_depts_ids->toArray());
                }
            }
            //没有数据集范围 默认只能看自己的
            if(count($user_ids) == 0){
                $builder->where('create_by', $user_id);
            }else{
                $builder->whereIn('create_by', $user_ids);
            }
        }

    }
}
